package com.fa4j.system.app.controller;

import com.fa4j.common.base.model.CommonState;
import com.fa4j.common.web.model.AppType;
import com.fa4j.system.app.AppClient;
import com.fa4j.system.app.repository.AppMenuRepository;
import com.fa4j.system.app.repository.AppRoleRepository;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Stream;


@Hidden
@Tag(name = "系统应用")
@RestController
@RequestMapping("system/app")
@RequiredArgsConstructor
public class AppResource implements AppClient {
    private final AppRoleRepository appRoleRepository;
    private final AppMenuRepository appMenuRepository;


    @Override
    public List<String> queryApisByRoleCode(String appType, String roleCode) {
        return appRoleRepository.findByAppAndCode(AppType.valueOf(appType), roleCode)
                .stream()
                .filter(r -> r.getState() == CommonState.ENABLE && r.getMenuIds() != null)
                .flatMap(r -> appMenuRepository.queryByIds(r.getMenuIds()).stream())
                .flatMap(m -> m.getApis() == null ? Stream.empty() : m.getApis().stream())
                .toList();
    }


}
